/*
 * Copyright (c) 2012-2014 Wind River Systems, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stdio.h>
#include <zephyr/kernel.h>
#include "hal/nrf_gpio.h"

/**
 * nRF54L15 DK – Pin Usage Summary (Debug Notes)
 * Port 0
 *         Pin 0–4: Available for general use.
 * Port 1
 *         Pin 0–1: Connected to 32.768 kHz crystal. ok
 *         Pin 2–3: Assigned to NFC.
 *         Pin 4–7: Assigned to UART. ok
 *         Pin 8–9: Available for general use.
 * Port 2
 *         Pin 0–5: Assigned to external memory. ok
 *         Pin 6–10: Available for general use.
 */

void gpio_output_test(void)
{
    uint8_t pin;

	// GPIO port 0 initialization. Pin 0–4
    for(pin = 0; pin < 5; pin++) {
        nrf_gpio_cfg_output(NRF_GPIO_PIN_MAP(0, pin));
    }

	// GPIO port 1 initialization. Pin 0–9
    for(pin = 0; pin < 10; pin++) {
        nrf_gpio_cfg_output(NRF_GPIO_PIN_MAP(1, pin));
    }

	// GPIO port 2 initialization. Pin 0–10
    for(pin = 0; pin < 11; pin++) {
        nrf_gpio_cfg_output(NRF_GPIO_PIN_MAP(2, pin));
    }

	while (1)
	{
		// GPIO output high
		for(pin = 0; pin < 5; pin++) {
			nrf_gpio_pin_set(NRF_GPIO_PIN_MAP(0, pin));
		}

		for(pin = 0; pin < 10; pin++) {
			nrf_gpio_pin_set(NRF_GPIO_PIN_MAP(1, pin));
		}

		for(pin = 0; pin < 11; pin++) {
			nrf_gpio_pin_set(NRF_GPIO_PIN_MAP(2, pin));
		}

		k_sleep(K_MSEC(1));

		// GPIO output low
		for(pin = 0; pin < 5; pin++) {
			nrf_gpio_pin_clear(NRF_GPIO_PIN_MAP(0, pin));
		}

		for(pin = 0; pin < 10; pin++) {
			nrf_gpio_pin_clear(NRF_GPIO_PIN_MAP(1, pin));
		}

		for(pin = 0; pin < 11; pin++) {
			nrf_gpio_pin_clear(NRF_GPIO_PIN_MAP(2, pin));
		}

		k_sleep(K_MSEC(1));
	}
}

void gpio_input_test(void)
{
    uint8_t pin;

    // GPIO port 0 initialization. Pin 0–4
    for(pin = 0; pin < 5; pin++) {
        nrf_gpio_cfg_input(NRF_GPIO_PIN_MAP(0, pin), NRF_GPIO_PIN_PULLUP);
    }

    // GPIO port 1 initialization. Pin 0–9
    for(pin = 0; pin < 10; pin++) {
        nrf_gpio_cfg_input(NRF_GPIO_PIN_MAP(1, pin), NRF_GPIO_PIN_PULLUP);
    }

    // GPIO port 2 initialization. Pin 0–10
    for(pin = 0; pin < 11; pin++) {
        nrf_gpio_cfg_input(NRF_GPIO_PIN_MAP(2, pin), NRF_GPIO_PIN_PULLUP);
    }

	uint8_t level;
	uint32_t cycle = 0;

    while (1)
    {
		printk("===== Cycle: %d ======\n", cycle++);
        printk("   PIN: 1 2 3 4 5 6 7 8 9 A B\n");
		printk("Port 0: ");
        for(pin = 0; pin < 5; pin++) {
            level = nrf_gpio_pin_read(NRF_GPIO_PIN_MAP(0, pin));
			printk("%d ",level);
        }
		printk("\n");
		printk("Port 1: ");
        for(pin = 0; pin < 10; pin++) {
            level = nrf_gpio_pin_read(NRF_GPIO_PIN_MAP(1, pin));
			printk("%d ",level);
        }
		printk("\n");
		printk("Port 2: ");
        for(pin = 0; pin < 11; pin++) {
            level = nrf_gpio_pin_read(NRF_GPIO_PIN_MAP(2, pin));
			printk("%d ",level);
        }
		printk("\n");

        k_sleep(K_MSEC(500));
    }
}

int main(void)
{

	//gpio_output_test();
	gpio_input_test();

	return 0;
}
